import Vue from 'vue'
import Router from 'vue-router'
import main from '../view/main.vue'
import login from '@/view/login.vue'
import CategoryEdit from '@/view/CategoryEdit'
import Categorylist from '@/view/Categorylist'
import ItemEdit from '@/view/ItemEdit'
import Itemlist from '@/view/Itemlist'
import Itemlists from '@/view/ItemNotlist'
import HeroEdit from '@/view/HerosEdit'
import Herolist from '@/view/Heroslist'
import HeroNotlist from '@/view/HerosNotlist'
import ArticleEdit from '@/view/ArticleEdit'
import Articlelist from '@/view/Articlelist'
import AdvertiseEdit from '@/view/AdvertiseEdit'
import Advertiselist from '@/view/Advertiselist'
import AdminuserEdit from '@/view/AdminuserEdit'
import Adminuserlist from '@/view/Adminuserlist'


Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/',
      name:'main',
      component: main,
      children:[
        {
          path:'/categories/create',
          component:CategoryEdit
        },
        {
          path:'/categories/list',
          component:Categorylist
        },
        {
          path:'/categories/edit/:id',
          component:CategoryEdit,
          props:true
        },
        {
          path:'/Item/create',
          component:ItemEdit
        },
        {
          path:'/Item/list',
          component:Itemlist
        },
        {
          path:'/Item/console',
          component:Itemlists
        },
        {
          path:'/Hero/create',
          component:HeroEdit
        },
        {
          path:'/Hero/list',
          component:Herolist
        },
        {
          path:'/Hero/lists',
          component:HeroNotlist
        },
        {
          path:'/Hero/edit/:id',
          component:HeroEdit,
          props:true
        },
        {
          path:'/Article/edit/:id',
          component:ArticleEdit,
          props:true
        },
        {
          path:'/Article/create',
          component:ArticleEdit
        },
        {
          path:'/Article/list',
          component:Articlelist
        },
        {
          path:'/Article/edit/:id',
          component:ArticleEdit,
          props:true
        },
        {
          path:'/Advertise/create',
          component:AdvertiseEdit
        },
        {
          path:'/Advertise/list',
          component:Advertiselist
        },
        {
          path:'/Advertise/edit/:id',
          component:AdvertiseEdit,
          props:true
        },
        {
          path:'/Adminuser/create',
          component:AdminuserEdit
        },
        {
          path:'/Adminuser/list',
          component:Adminuserlist
        },
        {
          path:'/Adminuser/edit/:id',
          component:AdminuserEdit,
          props:true
        },
      ]
    },
    {
      path:'/login',
      name:'login',
      component:login,
      meta:{
        isPublic:true
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token){
    return next('/login')
  }
  next()
})

export default router
